package org.common.utils.tool.window;
import org.common.utils.tool.StringTool;
import javax.swing.*;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.jar.Attributes;

/**
 * @author zhouzhibing
 * @date 2025/2/24
 */
public class VersionTool {

    public static void initVersionTitle(JFrame jFrame , String titleString) {
        try {
            Class<?> clzss =  VersionTool.class;
            String sourceName = clzss.getName().replace('.', '/').concat(".class");
            URL resource = clzss.getClassLoader().getResource(sourceName);
            if(resource != null) {
                URLConnection urlConnection = resource.openConnection();
                if(urlConnection instanceof JarURLConnection jarUrl) {
                    Attributes mainAttributes = jarUrl.getMainAttributes();
                    String versionTime = mainAttributes.getValue("Version-Time");
                    if(!StringTool.isEmpty(versionTime)) {
                        jFrame.setTitle(titleString + " | VersionTime: " +versionTime);
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
